package com.yzc.result;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import java.time.Instant;

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> {
    private int code;
    private String message;
    private T data;
    private final long timestamp = Instant.now().getEpochSecond();

    // 静态工厂方法
    public static <T> Result<T> of(ResultCodeEnum codeEnum, T data) {
        Result<T> result = new Result<>();
        result.code = codeEnum.getCode();
        result.message = codeEnum.getMessage();
        result.data = data;
        return result;
    }

    public static Result<Void> success() {
        return of(ResultCodeEnum.SUCCESS, null);
    }

    public static <T> Result<T> success(T data) {
        return of(ResultCodeEnum.SUCCESS, data);
    }

    public static <T> Result<T> fail(int code, String message) {
        Result<T> result = new Result<>();
        result.code = code;
        result.message = message;
        return result;
    }

    public static <T> Result<T> fail(ResultCodeEnum codeEnum) {
        return of(codeEnum, null);
    }
}